//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    acct_or_pwd_err: "账号密码错误",
    acct_placeholder: "邮箱",
    acct_value: "",
    pwd_placeholder: "密码",
    pwd_value: "",
    login_btn_text: "登录",
    login_btn_disabled: "",
    tips_text: "帐号或是密码错了",
    tips_hide: "hide",
    disabled: true,
  },
  //事件处理函数
  bindViewTap: function() {},

  // 页面加载
  onLoad: function() {},

  // 账号输入
  bindAcctKeyInput: function(event) {
    this.setData({
      acct_value: event.detail.value,
      tips_hide: "hide"
    });
    this.checkLoginBtnDisabledStatus();
  },

  // 密码输入
  bindPwdKeyInput: function(event) {
    this.setData({
      pwd_value: event.detail.value,
      tips_hide: "hide",
    });
    this.checkLoginBtnDisabledStatus();
  },

  // 检查登陆按钮的disabled状态
  checkLoginBtnDisabledStatus: function() {
    if (this.data.acct_value == "" || this.data.pwd_value == "") {
      this.setData({
        disabled: true
      });
    } else {
      this.setData({
        disabled: false
      });
    }
  },

  // 登陆按钮点击
  login: function(event) {
    if (this.data.acct_value == "") {
      // 显示错误提示
      this.setData({
        tips_hide: ""
      });
    } else if (this.data.pwd_value == "") {
      // 显示错误提示
      this.setData({
        tips_hide: ""
      });
    } else {
      let that = this;
      util.request('/api/login', {
        email: this.data.acct_value,
        password: this.data.pwd_value
      }, function(res) {
        // 登陆成功
        if (res.data.errCode != 0) {
          // 显示错误提示
          that.setData({
            tips_hide: ""
          });
          return false;
        }
        setTimeout(function(){
          // 跳转到首页
          wx.switchTab({
            url: '/pages/task/index',
          });
        }, 1000);
      }, {"loadingText":"登陆中..."});
    }
  }
})